package com.zx.jhpt.login;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.framework.core.util.DatetimeUtils;
import com.zx.framework.core.util.FileUtil;
import com.zx.framework.core.util.SecurityUtil;
import com.zx.framework.core.util.StringUtil;
import com.zx.framework.plat.login.LoginAdapterController;
import com.zx.jhpt.service.EnclosureService;

@Controller
@RequestMapping("/util")
public class UtilController extends LoginAdapterController {

    private static final Logger LOGGER = Logger.getLogger(UtilController.class);
    
    @Resource
    private String jhptUpload;
    @Resource
    private EnclosureService enclosureService;

    //文件打包状态
    private static boolean FLAG = true;
    
    /**
     * 打包所有附件
     * @param hcsj 核查月份
     * @return
     */
    @SuppressWarnings("rawtypes")
	@RequestMapping("/pack")
	@ResponseBody
	public Map<String, Object> pack(String hcsj){
    	if (StringUtil.isEmpty(hcsj)) {
    		return clientMap().failure().message("请选择核查月份！").getMap();
		}
    	if (FLAG) {
    		FLAG = false;
    		try {
    			String temp_zip_path = jhptUpload+"回执文件";
    			File file = new File(temp_zip_path);
    			//删除目录及所有文件
    			FileUtils.deleteDirectory(file);
    			
    			if (!file.isDirectory()) {
    				file.mkdirs();
    			}
    			
    			List<Map> datas = enclosureService.getEnclByHcsj(hcsj);
    			if (null == datas || datas.isEmpty()) {
    				return clientMap().failure().message("没有该核查月份("+hcsj+")的回执附件！").getMap();
    			}
    			String exp = "";
    			for (Map map : datas) {
    				exp = String.valueOf(map.get("FILENAME"));
    				exp = exp.substring(exp.lastIndexOf("."), exp.length());
    				FileUtils.copyFile(
    						new File(jhptUpload+File.separator+String.valueOf(map.get("URL"))+File.separator+String.valueOf(map.get("PKID"))+".enc"), 
    						new File(temp_zip_path+File.separator+String.valueOf(map.get("FXID"))+"_"+String.valueOf(map.get("NAME"))+"_"+String.valueOf(map.get("HCSJ"))+exp)
    						);
    			}
    			String fileName = "核查日期"+hcsj+"("+DatetimeUtils.dateToString(new Date(), "yyyy年MM月dd日HH时mm分ss秒")+").zip";
    			String descPath = jhptUpload+fileName;
    			FileUtil.zip(temp_zip_path, descPath);
    			return clientMap().success().message("打包成功,打包目录为("+descPath+")").getMap();
    		} catch (Exception e) {
    			LOGGER.error("", e);
    			return clientMap().failure().message("打包失败！").getMap();
    		} finally {
    			FLAG = true;
    		}
		}else{
			//如果其他线程正在打包，提示信息
			return clientMap().failure().message("其他用户正在打包请稍后！").getMap();
		}
    	
    }
    
    public static void main(String[] args) {
//    	String temp_zip_path = "d:/jhptUpload"+File.separator+"temp-zip";
//		File file = new File(temp_zip_path);
//		if (!file.isDirectory()) {
//			file.mkdirs();
//		}
//		//删除目录下所有文件
//		try {
//			FileUtils.deleteDirectory(file);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    	
//    	FileUtils.copyFile(new File("D:\\aa\\A123.xls"), new File("D:\\aa\\bb\\A123.xls"));
    	try {
    		FileUtil.zip("D:\\jhptUpload\\temp-zip", "D:\\jhptUpload\\核查日期2016-02(2016年03月11日15时20分30秒).zip");
//    		FileUtils.deleteDirectory(new File("D:\\aa\\bb"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
